 import Crypto, { enc } from 'crypto-js';
 
 // 定义一个函数，接收base64字符串和文件名作为参数
 export function dataURLtoFile(dataurl, filename) {
    // 分离数据类型和实际的 base64 数据
    const arr = dataurl.split(',');
    // 获取 mime 类型
    const mime = arr[0].match(/:(.*?);/)[1];
    // 解码 Base64 字符串到二进制数据
    const decodedData = atob(arr[1]);
    // 创建一个Uint8Array视图来处理二进制数据
    const buffer = new Uint8Array(decodedData.length);
    for (let i = 0; i < decodedData.length; i++) {
      buffer[i] = decodedData.charCodeAt(i);
    }
    // 使用 Blob 对象构造一个文件
    const blob = new Blob([buffer], { type: mime });
    // 构造一个 File 对象
    return new File([blob], filename, { type: mime });
  }
 //将文件对象转换成base64图片
export function fileToDataURL(file) {
  return new Promise((resolve, reject) => {  
    const reader = new FileReader();  
    reader.onload = function(event) {  
        resolve(event.target.result);  
    };  
    reader.onerror = function(error) {  
        reject(error);  
    };  
    reader.readAsDataURL(file);  
});  
}
  //加密
export function SecureSS() {
   // 自定义加密和解密方法
function encryptData(data) {
  const key = import.meta.env.VITE_KEY; // 定义你的密钥
  const encrypted = Crypto.AES.encrypt(JSON.stringify(data), key).toString();
  return encrypted;
}

function decryptData(encryptedData) {
  const key = import.meta.env.VITE_KEY;
  const bytes = Crypto.AES.decrypt(encryptedData, key);
  const decryptedData = JSON.parse(bytes.toString(Crypto.enc.Utf8));
  return decryptedData;
}
  // 修改默认的读写方法以添加加密
    const customStorage = {
    getItem: (key) => {
      const encrypted = window.sessionStorage.getItem(key);
      const decryptedData = decryptData(encrypted);
      return encrypted ? decryptedData : null;
    },
    setItem: (key, value) => {
      const encrypted = encryptData(value);
      window.sessionStorage.setItem(key, encrypted);
    },
};
return customStorage;
}
//对象数组去重
export function unique(arr,prop) {
  const map = new Map();
  return arr.filter(item => !map.has(item[prop]) && map.set(item[prop], true)); 
}



function calculateSimilarity(str1, str2) {
  let maxLength = 0;
  let length = 0;
  for (let i = 0; i < str1.length; i++) {
      for (let j = 0; j < str2.length; j++) {
          let k = 0;
          while (i + k < str1.length && j + k < str2.length && str1[i + k] === str2[j + k]) {
              k++;
          }
          if (k > maxLength) {
              maxLength = k;
              length = 1;
          } else if (k === maxLength) {
              length++;
          }
      }
  }
  return maxLength * length;
}
export function matchAndReplace(arr1, arr2) {
  const result = [];
  for (let item2 of arr2) {
      let maxSimilarity = 0;
      let mostSimilarItem = null;     
      for (let item1 of arr1) {
          let name = item1.name.split('.')[0];
          const similarity = calculateSimilarity(name, item2.title);
          if (similarity > maxSimilarity) {
              maxSimilarity = similarity;
              mostSimilarItem = item1;
          }
      }     
      if (mostSimilarItem) {
          mostSimilarItem.match = item2.img;
          result.push(mostSimilarItem);
      }    
      // 从arr1中移除已匹配的项
      arr1 = arr1.filter(item => item !== mostSimilarItem);
  }
  return result;
}
//修改文件对象的名字
export function renameFile(file, newFileName) {
  const newFile = new File([file], newFileName, { type: file.type });
  return newFile;
}
export function getPastDaysAsStrings(days) {
  // 获取当前日期
  let  today = new Date();
  // 创建一个数组来存储过去五天的日期字符串
  let pastFiveDays = [];
  for (let  i = 0; i < days; i++) {
    // 减去一天
    let pastDay = new Date(today.getTime() - (i * 24 * 60 * 60 * 1000));
    // 将日期格式化为'YYYY-MM-DD'形式的字符串并添加到数组中
    let formattedDate = pastDay.toISOString().slice(0, 10);
    pastFiveDays.push(formattedDate);
  }
  return pastFiveDays.reverse();
}
